
Page({
    data: {
         src:'',
         isNew:false

        
    },
    onLoad:function(opt){

       if (opt.id) {
          this.setData({
            id:opt.id
          })
       }
    
       getApp().getUserInfo().then(res=>{

       var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
        console.log('res*****')  
        console.log(res)  
        if (res.phone&&res.phone.length==11) {
             if (this.data.id) {
                wx.showToast({
                title: '您不是新用户,邀请新人尽享优惠',
                icon: 'none',
                duration: 5000
              })
             }
            this.setData({
              isNew:false
              
            }) 
        }else{
           this.setData({
              isNew:true
             
            }) 
        }

       this.setData({
        userInfo:res,
        header:header, 
       
       })  
        // this.getCoupon();
     })
     
    },
    onShow:function(){
      
    },
    getCoupon(){
       if(this.data.id==this.data.userInfo.openid||!this.data.id){
         this.setData({
              isNew:false
        }) 
         console.log('不能自己邀请自己')
         return
       }
       wx.request({
            url: getApp().globalData.baseUrl + '/yx/invitation/addInvitation?openid='+this.data.id+'&invOpenid='+this.data.userInfo.openid,
            method:"post",
            header: this.data.header,
            success: function(res) {
            if (res.data.code==200) {
              wx.showModal({
              title: '温馨提示',
              content: res.data.des+',去查看',
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                 wx.navigateTo({
                 url: '/pages/mineCoupon/mineCoupon'
                })  
                
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })

            }else{
              wx.showToast({
              title: res.data.des,
              icon: 'none',
              duration: 2000
            })

            }

             console.log(res)
            
           
         }
       })
    },
     //更新用户信息
    getPhoneNumber (e) {
       console.log(e.detail.errMsg)
       console.log(e.detail.iv)
       console.log(e.detail.encryptedData)
      var userInfo= wx.getStorageSync('userInfo');
       var _self=this;

        wx.login({
           success (res) {
            if (res.code) {
                     wx.request({
                      url:getApp().globalData.baseUrl+'/wx/user/getUserPhone',
                      method:"post",
                      data:{
                        code:res.code,
                        encryptedData:e.detail.encryptedData,
                        iv:e.detail.iv,
                        openid:userInfo.openid
                      },
                      header:{
                         'openid':userInfo.openid, //公用请求头
                         'content-type': 'application/json'
                      },
                      success: function(res) { 
                      userInfo.phone=res.data.obj;
                      wx.setStorageSync('userInfo',userInfo);
                      console.log(res.data.obj)  
                      _self.getCoupon();
                      _self.triggerEvent('loginSuccess')
                   }
                 })  
            }
          }
        })

    },
    bindGetUserInfo (e) {
   
    console.log(e)
    console.log(e.detail.userInfo);
    if (!e.detail.userInfo) {
       wx.showToast({
          title: '需要授权之后才可以享受服务',
          icon:'none',
          duration: 2000
        })
      return
    }
    var userInfo= wx.getStorageSync('userInfo');
    var address= wx.getStorageSync('address');
   

    var json=e.detail.userInfo;
    json.openid=userInfo.openid;
    if (address) {
       json.city=address.city;
       json.province=address.province;
    }
    console.log('userInfo')
    console.log(userInfo)
    var _self=this;
     wx.request({
            url:getApp().globalData.baseUrl+'/wx/user/update',
            method:"post",
            header: this.data.header,
            data:e.detail.userInfo,
            success: function(res) {
            if(res.data.code==200){
               _self.getCoupon();
               wx.setStorageSync('userInfo',json);
               _self.triggerEvent('loginSuccess')
            }  
         }
       })
    },
     onShareAppMessage:function(){
        return {
              title: '邀请新人尽享优惠',
              path: '/pages/invitation/invitation?id='+this.data.userInfo.openid+"&type=share"
        }
    },
   
});